﻿
namespace HSS.Xaml.UI.Data
{
	using System;
	using System.Windows.Data;

	/// <summary>
	/// Two way IValueConverter that lets you bind the inverse of a boolean property
	/// to a dependency property
	/// </summary>
	public class NotOperatorValueConverter : IValueConverter
	{
		/// <summary>
		/// Invert the provided boolean value.
		/// </summary>
		/// <param name="value">The boolean value to invert.</param>
		/// <param name="targetType">Not used.</param>
		/// <param name="parameter">Not used.</param>
		/// <param name="culture">Not used.</param>
		/// <returns>The inverted boolean value.</returns>
		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			return !((bool)value);
		}
		/// <summary>
		/// Invert the provided boolean value.
		/// </summary>
		/// <param name="value">The boolean value to invert.</param>
		/// <param name="targetType">Not used.</param>
		/// <param name="parameter">Not used.</param>
		/// <param name="culture">Not used.</param>
		/// <returns>The inverted boolean value.</returns>
		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			return !AssureBoolean(value, false);
		}
		/// <summary>
		/// Ensures the provided value is a boolean.
		/// </summary>
		/// <param name="value">The value to ensure is a boolean.</param>
		/// <param name="defaultIfNull">The default value to return if the value is not a boolean.</param>
		/// <returns>The provided value as a boolean.</returns>
		public static bool AssureBoolean(object value, bool defaultIfNull)
		{
			if (!(value is bool?))
				return (bool)value;
			bool? nullable = (bool?)value;
			if (nullable.HasValue)
				return nullable.Value;
			return defaultIfNull;
		}
	}
}
